package qc;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import org.quickconnect.ControlObject;

import ui.MainFrame;

import com.mysql.jdbc.Connection;

import controller.ServerConnectionHandler;

public class RecordDownloadBCO implements ControlObject{

	@Override
	public Object handleIt(ArrayList<Object> arg0) {
		
		ServerConnectionHandler connection = (ServerConnectionHandler) arg0.get(0);
		HashMap parameters = (HashMap) arg0.get(arg0.size()-1);
		String fileID = (String) parameters.get("fileId");
		String sessionID = (String) parameters.get("sessionId");
		
		Connection con = (Connection) MainFrame.mainFrame.getController().getConnectionPool().getConnection();
		java.sql.PreparedStatement insert = null;
		
		try {
			insert = con.prepareStatement("INSERT INTO Downloads (UserID, SessionID, FileID, DownloadTime) VALUES ((SELECT UserID FROM User WHERE UserName = ?), (SELECT SessionID FROM Session WHERE SessionNumber = ?), (SELECT FileID FROM File WHERE FileNumber = ? AND FileActiveFlag = 1), ?)");
			insert.setString(1, connection.getUser().getUsername());
			insert.setString(2, sessionID);
			insert.setString(3, fileID);
			insert.setTimestamp(4, new java.sql.Timestamp(System.currentTimeMillis()));
			insert.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return parameters;
	}

}
